package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者:hfj
 * 功能:角色管理的增删改查功能
 * 日期: 2024/4/18 14:49
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("/admin/acl/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     *
     * @param current 当前页
     * @param limit 每页记录数
     * @param roleQueryVo 角色查询类
     * @return 角色条件分页查询
     */
    @ApiOperation("角色条件分页查询")
    @GetMapping("/{current}/{limit}")
    public Result pageList(@PathVariable Long current, @PathVariable Long limit, RoleQueryVo roleQueryVo){
        //1.创建page对象,传递当前页和每页记录数
        Page<Role> pageParam = new Page<>(current,limit);

        //2.调用service的方法实现条件分页查询,返回分页对象
        IPage<Role> pageModel = roleService.selectRolePage(pageParam,roleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     *
     * @param id 角色id
     * @return 根据id查询角色
     */
    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id){
        Role role = roleService.getById(id);
        return Result.ok(role);
    }


    /**
     *
     * @param role 角色实体
     * @return 添加角色
     */
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result save(@RequestBody Role role){
        boolean is_success = roleService.save(role);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }


    /**
     *
     * @param role 角色实体
     * @return 修改角色
     */
    @ApiOperation("修改角色")
    @PutMapping("/update")
    public Result update(@RequestBody Role role){
        boolean is_success = roleService.updateById(role);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     *
     * @param id 角色id
     * @return 根据id删除角色
     */
    @ApiOperation(value = "根据id删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean is_success = roleService.removeById(id);
        if (is_success){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }

    /**
     *
     * @param idList 角色id集合
     * @return
     */
    @ApiOperation(value = "批量删除角色")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        roleService.removeByIds(idList);
        return Result.ok(null);
    }

}
